# Author    : CoffeeChicken
# Date      : 2022/1/2 21:52
# Function  : 演示 从数据库获取数据

import requests
import json
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.globals import ThemeType

url = "https://y.dxy.cn/papi/hospital/detailInfo?hospital_id=166"

response = requests.get("http://192.168.134.161:8081/hospitals")

hospitals = json.loads(response.text)['hospitals']

# 获取各省市全部医院数据
all_hospital = {}

for hospital in hospitals:
    all_hospital[hospital['province']] = all_hospital.get(hospital['province'], 0) + 1
    # print(hospital)

all_hospital_keys = all_hospital.keys()
all_hospital_values = all_hospital.values()

c = (
    Map(init_opts=opts.InitOpts(width="1600px",
                                height="800px",
                                ))
        .add("医疗数据", [list(z) for z in zip(all_hospital_keys, all_hospital_values)], "china")
        .set_global_opts(title_opts=opts.TitleOpts(title="各省三级与二级医院数量"), visualmap_opts=opts.VisualMapOpts(max_=300))
        .render("province_hospital_number.html")
)

# 获取各省市床位数
all_beds = {}
for hospital in hospitals:
    if hospital['bedNum'] != -1:
        all_beds[hospital['province']] = all_beds.get(hospital['province'], 0) + int(hospital['bedNum'])

print(all_beds)
all_beds_keys = all_beds.keys()
all_beds_values = all_beds.values()

all_beds_chart = (
    Map(init_opts=opts.InitOpts(width="1600px",
                                height="700px", theme=ThemeType.LIGHT))
        .add("床位数量", [list(z) for z in zip(all_beds_keys, all_beds_values)], "china")
        .set_global_opts(title_opts=opts.TitleOpts(title="各省三级与二级医院床位数量"),
                         visualmap_opts=opts.VisualMapOpts(max_=230000))
        .render("province_hospital_bed_number.html")
)

# 获取年住院人数
year_live_hospital = {}
for hospital in hospitals:
    if hospital['yearHospitalNum'] != "-1":
        year_live_hospital[hospital['province']] = year_live_hospital.get(hospital['province'], 0) + int(
            hospital['yearHospitalNum'])

# print(year_live_hospital)
year_live_hospital_keys = year_live_hospital.keys()
year_live_hospital_values = year_live_hospital.values()
year_live_hospital_chart = (
    Map(init_opts=opts.InitOpts(width="1600px",
                                height="700px", theme=ThemeType.WONDERLAND))
        .add("年住院人数", [list(z) for z in zip(year_live_hospital_keys, year_live_hospital_values)], "china")
        .set_global_opts(title_opts=opts.TitleOpts(title="各省三级与二级医院年住院人数"),
                         visualmap_opts=opts.VisualMapOpts(max_=3000000))
        .render("year_live_hospital.html")
)


# 获取年急诊人数
year_emergency_number = {}
for hospital in hospitals:
    if hospital['yearOutPatient'] != "-1":
        year_emergency_number[hospital['province']] = year_emergency_number.get(hospital['province'], 0) + int(
            hospital['yearOutPatient'])

# print(year_emergency_number)
year_emergency_number_keys = year_emergency_number.keys()
year_emergency_number_values = year_emergency_number.values()
year_emergency_number_chart = (
    Map(init_opts=opts.InitOpts(width="1600px",
                                height="700px", theme=ThemeType.WONDERLAND))
        .add("年急诊数", [list(z) for z in zip(year_emergency_number_keys, year_emergency_number_values)], "china")
        .set_global_opts(title_opts=opts.TitleOpts(title="各省三级与二级医院年急诊人数"),
                         visualmap_opts=opts.VisualMapOpts(max_=30000000))
        .render("year_emergency_number.html")
)


# 获取年急诊人数
all_employee_number = {}
for hospital in hospitals:
    if hospital['yearOutPatient'] != "-1":
        all_employee_number[hospital['province']] = all_employee_number.get(hospital['province'], 0) + int(
            hospital['yearOutPatient'])

print(all_employee_number)
all_employee_number_keys = all_employee_number.keys()
all_employee_number_values = all_employee_number.values()
year_emergency_number_chart = (
    Map(init_opts=opts.InitOpts(width="1600px",
                                height="700px", theme=ThemeType.WONDERLAND))
        .add("员工总数", [list(z) for z in zip(all_employee_number_keys, all_employee_number_values)], "china")
        .set_global_opts(title_opts=opts.TitleOpts(title="各省医院员工总数"),
                         visualmap_opts=opts.VisualMapOpts(max_=30000000))
        .render("all_employee_number.html")
)


